package cc.sheji.controller.admin;


import cc.sheji.Service.DeviceService;
import cc.sheji.domain.Device;
import cc.sheji.domain.DeviceQueryCriteria;
import cc.sheji.domain.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@CrossOrigin
@RestController
@RequestMapping(value = "/admin/v1/device")
public class AdminDeviceController {

    @Autowired
    DeviceService deviceSevice;

    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(deviceSevice.get(id));
    }

    @PostMapping()
    public JsonResult create(@RequestBody @Validated Device entity) {
        entity.setId(null);
        return JsonResult.Success(deviceSevice.create(entity));
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @Valid  Device entity) {
        entity.setId(id);
        return JsonResult.Success(deviceSevice.update(entity));
    }

    @GetMapping()
    public JsonResult get(DeviceQueryCriteria criteria , Pageable pageable) throws InterruptedException {
        return JsonResult.Success(deviceSevice.list(criteria,pageable));
    }
}
